<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 控制器验证码退款页面
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\common\model\KuaiShouCodeModel;
use Exception;
use think\addons\Service;
use think\exception\PDOException;
use think\facade\Cache;
use think\response\Json;

class KuaiShouCode extends Adminbase
{
    //初始化
    protected function initialize()
    {
        parent::initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
        $this->modelClass = new KuaiShouCodeModel;
    }

    /**
     * 后台首页
     * @return mixed|Json
     */
    public function index()
    {
        if ($this->request->isAjax()) {

            $limit = $this->request->get("limit/d", 999999);
            $page = $this->request->get("page/d", 1);
            $search = $this->request->get("search", '');
            $where[] = [
                KuaiShouCodeModel::IS_DELETE, '=', 0,
            ];
            if ($search) {
                $searcharr = [
                    KuaiShouCodeModel::REALNAME,
                    KuaiShouCodeModel::BANK_CARD_NUMBER,
                    KuaiShouCodeModel::STATUS,
                ];
                $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
            }
            $count = $this->modelClass
                ->where($where)
                ->count();
            $data = $this->modelClass
                ->where($where)
                ->order('id', 'DESC')
                ->page($page, $limit)
                ->select();
            if (!empty($data)) {
                foreach ($data as $item) {
                    $item->url = createCodeUrl($item->id);
                }
            }
            $result = ["code" => 0, 'count' => $count, 'data' => $data];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 新增退款记录
     * @return mixed|void
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a", [], 'strip_tags');
            if (empty($params)) $this->error('参数不能为空');
            $result = $this->validate($params, 'app\admin\validate\KuaiShouCodeValidate');
            if (true !== $result) {
                $this->error($result);
            }
            $kuaiShouCodeModel = new KuaiShouCodeModel();
            $res = $kuaiShouCodeModel->save($params);
            $res ? $this->success('新增成功') : $this->error('新增失败');
        }
        return $this->fetch();

    }

    /**
     * 更新操作
     * @return mixed
     */
    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        $kuaiShouCodeModel = new KuaiShouCodeModel();
        $info = $kuaiShouCodeModel->getInfoById($id);
        if (is_null($info)) $this->error('信息不存在');
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a", [], 'strip_tags');
            if (empty($params)) $this->error('参数不能为空');
            $result = $this->validate($params, 'app\admin\validate\KuaiShouCodeValidate');
            if (true !== $result) {
                $this->error($result);
            }
            $params[KuaiShouCodeModel::UPDATE_TIME] = date('Y-m-d H:i:s');
            $res = $info->save($params);
            $res ? $this->success('更新成功') : $this->error('更新失败');
        }
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch();
    }

    /**
     * 删除数据
     * @return void
     */
    public function del()
    {
        $ids = $this->request->param('id/a', null);
        if (empty($ids)) {
            $this->error('参数错误！');
        }
        if (!is_array($ids)) {
            $ids = [0 => $ids];
        }
        $where[] = [KuaiShouCodeModel::ID, 'in', $ids];
        $list = KuaiShouCodeModel::where($where)->select();
        $count = 0;
        $time = date('Y-m-d H:i:s');
        try {
            foreach ($list as $k => $v) {
                $count += $v->save([
                    KuaiShouCodeModel::IS_DELETE => 1,
                    KuaiShouCodeModel::DELETE_TIME => $time,
                ]);
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        if ($count) {
            $this->success("操作成功！");
        }
        $this->error('没有数据删除！');
    }

}
